//
// Created by 66477 on 2022/9/28.
//

#ifndef DATA_STRUCTURE_STACK1_H
#define DATA_STRUCTURE_STACK1_H

#include "List4_1.h"

class Stack4_1 {
private:
    List4_1 *list = nullptr;
    Node4_1 *top = new Node4_1();
public:
    Stack4_1(List4_1 *list) : list(list) {}

    void push(char x) {
        Node4_1 *n = new Node4_1(x);
        n->setNext(top);
        top = n;
    }

    void pop() {
        Node4_1 *p = top;
        top = top->getNext();
        delete p;
    }

    string linkIsSymmetries() {
        Node4_1 *c;
        bool isHarfFront = true;
        while ((c = list->get()) != nullptr) {//遍历到链表尾
            if (c == list->getHalf()->getNext()) {//当条过半的时候
                isHarfFront = false;//过半开启pop模式
                if (list->getHalf()->getData() == '1') {//奇数的时候跳过中间数
                    if ((c = list->get()) == nullptr) {//排除只有一个字的时候
                        return "true";
                    }
                }
            }
            if (isHarfFront) {
                push(c->getData());//入栈
            } else {
                if (top->getData() == c->getData()) {//栈顶元素值相等时出栈
                    pop();
                } else {
                    return "false";
                }
            }
        }
        return "true";
    }

};

#endif //DATA_STRUCTURE_STACK1_H
